import 'dart:async';

import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import 'package:rxdart/rxdart.dart';

class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        actions: <Widget>[
          IconSource("RxDart","19"),
        ],
      ),
      body:RxDartDemo() ,
    );
  }
}
class RxDartDemo extends StatefulWidget {
  @override
  _RxDartDemoState createState() => _RxDartDemoState();
}

class _RxDartDemoState extends State<RxDartDemo> {

  PublishSubject subject;
  String _log="...";
  @override
  initState(){
    super.initState();
    subject=PublishSubject<String>();
    subject.debounceTime(Duration(milliseconds: 500)).listen((value){
      setState(() {
        _log=value;
      });
    });
  }

  _onChanged(value){
    subject.add(value);
  }
  @override
  void dispose() {
    super.dispose();
    subject.close();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8.0),
      child:Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              onChanged:_onChanged,
              decoration: InputDecoration(
                hintText: "请输入",
                icon: Icon(Icons.person),
              ),
            ),
            ListTile(
              title: Text("延迟500毫秒判断："),
              subtitle: Text(_log),
            ),
          ],
        ),
      ),
    );
  }
}


